package com.example.juc.until;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class ForkJoinNewTest {
    public static void test1(){
         long sum=0;
         //普通计算
        long s = System.currentTimeMillis();
        for (long i = 1L; i <=100_0000_0000L ; i++) {
             sum+=i;
        }
        long e = System.currentTimeMillis();
        System.out.println("结果："+sum +"时间："+(e-s));
    }

    public static void test2() throws ExecutionException, InterruptedException {
        long s = System.currentTimeMillis();
        //ForkJoin计算
        ForkJoinPool forkJoinPool =new ForkJoinPool();
        ForkJoinTask<Long> forkJoinTest = new ForkJoinTest(1L, 100_0000_0000L);
        ForkJoinTask<Long> submit = forkJoinPool.submit(forkJoinTest);
        Long sum = submit.get();
        long e = System.currentTimeMillis();
        System.out.println("结果："+sum +"时间："+(e-s));
    }
    public static void test3(){
        long s = System.currentTimeMillis();
        //并行流计算
        long sum = LongStream.rangeClosed(0L, 100_0000_0000L).parallel().reduce(0, Long::sum);
        long e = System.currentTimeMillis();
        System.out.println("结果："+sum +"时间："+(e-s));
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
       // test1(); //结果：-5340232216128654848时间：5207
     // test2();   //结果：-5340232216128654848时间：38189
      //test3(); // 结果：-5340232216128654848时间：967

    }
}
